package com.example.springboot.processor;

import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

/**
 * 允许定制应用的上下文的应用环境优于应用的上下文之前被刷新。（意思就是在spring上下文构建之前可以设置一些系统配置） EnvironmentPostProcessor的实现类必须要在META-INF/spring.factories文件中去注册，并且注册的是全类名。
 * 鼓励EnvironmentPostProcessor处理器检测Org.springframework.core.Ordered注解，这样相应的实例也会按照@Order注解的顺序去被调用。
 */
@Order(value = 10)
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

  @Override
  public void postProcessEnvironment(ConfigurableEnvironment environment,
      SpringApplication application) {
    try {
      InputStream inputStream = new FileInputStream(
          "D:\\javaDemo\\demo\\springboot\\src\\main\\java\\springboot.properties");
      Properties properties = new Properties();
      properties.load(inputStream);
      PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("my",
          properties);
      environment.getPropertySources().addLast(propertiesPropertySource);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
